package com.zhongxi.guoyuan.view.beautifulStore;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.gift.NoviceItemData;
import com.zhongxi.guoyuan.view.personalCenter.BeautifulStoreOrderActivity;

public class CommentSuccessActivity extends BaseActivity {

    private int pay;
    private NoviceItemData data;
    private TextView tvSurePay;
    private TextView tvContent;
    private ImageView commentBack;
    private TextView price;
    private TextView name;
    private TextView content;
    private TextView time;
    private TextView condition;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.dialog_comment_success);

        data = (NoviceItemData) getIntent().getSerializableExtra("data");
        pay = getIntent().getIntExtra("pay", 0);
        tvSurePay = (TextView) findViewById(R.id.tvSure);
        tvContent = (TextView) findViewById(R.id.tvContent);
        commentBack = (ImageView) findViewById(R.id.comment_success_back);

        price = (TextView) findViewById(R.id.comment_share_price);
        name = (TextView) findViewById(R.id.comment_share_name);
        content = (TextView) findViewById(R.id.comment_share_content);
        time = (TextView) findViewById(R.id.commemt_share_time);
        condition = (TextView) findViewById(R.id.comment_share_condition);

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        commentBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BeautifulStoreOrderActivity.class);
                intent.putExtra("commentCode", 1);
                startActivity(intent);
                finish();
            }
        });
        tvSurePay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != popupWindow && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    initPopuWindow();
                }
            }
        });
        tvContent.setText(pay + "银币");
    }

    @Override
    protected void initData() {
        super.initData();
//        if (data != null) {
//            if (data.getShareKey() != null) {
//                price.setText(Integer.parseInt(data.getActivity().getAprice()) * 0.05 + "元");
//                name.setText(data.getTitle());
//                content.setText(data.getName());
//                time.setText("领取时间：" + data.getStime().substring(0, 10) + "到" + data.getEtime().substring(0, 10));
//                if (data.getCurrencytype() == "1") {
//                    condition.setText(Integer.parseInt(data.getActivity().getAprice()) * 10 + "美币");
//                } else {
//                    condition.setText(Integer.parseInt(data.getActivity().getAprice()) + "银币");
//                }
//            }
//        }
    }

    private PopupWindow popupWindow;
    private View popupWindow_view;
    private SHARE_MEDIA platform;

    /**
     * 弹出分享界面
     */
    private void initPopuWindow() {
        popupWindow_view = getLayoutInflater().inflate(R.layout.pop_shared, null, false);
        popupWindow = new PopupWindow(popupWindow_view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(tvSurePay, Gravity.BOTTOM, 0, 0);
        TextView tv_cancle = (TextView) popupWindow_view.findViewById(R.id.tv_cancle);
        LinearLayout ll_qq = (LinearLayout) popupWindow_view.findViewById(R.id.ll_qq);
        LinearLayout ll_wechat = (LinearLayout) popupWindow_view.findViewById(R.id.ll_wechat);
        LinearLayout ll_qzone = (LinearLayout) popupWindow_view.findViewById(R.id.ll_qzone);
        LinearLayout ll_friendscircle = (LinearLayout) popupWindow_view.findViewById(R.id.ll_friendscircle);
        View.OnClickListener shareClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_qq:
                        platform = SHARE_MEDIA.QQ;
                        break;
                    case R.id.ll_wechat:
                        platform = SHARE_MEDIA.WEIXIN;
                        break;
                    case R.id.ll_qzone:
                        platform = SHARE_MEDIA.QZONE;
                        break;
                    case R.id.ll_friendscircle:
                        platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                        break;
                }
//                new ShareAction(CommentSuccessActivity.this).setPlatform(platform).setCallback(umShareListener).withTitle(data.getTitle()).withMedia(new UMImage(CommentSuccessActivity.this,R.mipmap.logo))
//                        .withText(data.getName()).withTargetUrl(Url.BASE_URL + "/activity/giftpacks/index?sharekey=" + data.getShareKey())
//                        .share();
//                popupWindow.dismiss();

            }
        };
        ll_qq.setOnClickListener(shareClickListener);
        ll_wechat.setOnClickListener(shareClickListener);
        ll_qzone.setOnClickListener(shareClickListener);
        ll_friendscircle.setOnClickListener(shareClickListener);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                showShortToast("收藏成功啦");
            } else {
                showShortToast("分享成功了!!");
            }
            finish();
            Intent intent = new Intent(context, BeautifulStoreOrderActivity.class);
            intent.putExtra("commentCode", 1);
            startActivity(intent);
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            showShortToast("分享失败啦");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            showShortToast("分享取消了");
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(context, BeautifulStoreOrderActivity.class);
        intent.putExtra("commentCode", 1);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
